<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="4">
        <el-input placeholder="请输入图书名称" clearable v-model="queryForm.username"></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="query">搜索</el-button>
      <el-button type="primary" @click="handleAdd">添加</el-button>
    </el-row>
    <el-row class="header">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="supply.name" label="供应商" />
        <el-table-column prop="accessoriesTypes.name" label="配件类别" />
        <el-table-column prop="name" label="配件名称" />
        <el-table-column prop="company.companyName" label="所属公司" />
        <el-table-column prop="des" label="配件分类描述" />
        <el-table-column prop="total" label="采购数量" />
        <el-table-column prop="price" label="采购单价" />
        <el-table-column prop="aaa" label="总价" :formatter="calculate"  />
        <el-table-column prop="createTime" label="创建时间" :formatter="formatTime" />
        <!--        <el-table-column prop="company.companyName" label="配件分类所属公司" />-->
        <el-table-column prop="purchase" label="采购状态" :formatter="formatPurchase" />
        <el-table-column prop="status" label="审核状态" :formatter="formatStatus" />
        <el-table-column label="操作" width="100">
          <template #default="{ row }">
            <el-button type="primary" circle :icon="Delete" @click="removeById(row)"></el-button>
            <el-button type="danger" circle :icon="Edit" @click="handleEdit(row)"></el-button>
          </template>
        </el-table-column>
        <el-table-column label="采购操作" width="115">
          <template #default="{ row }">
            <el-button type="success" @click="Purchase(row)" :disabled="row.purchase == 1">完成采购</el-button>
          </template>
        </el-table-column>
        <el-table-column label="审核" width="115">
          <template #default="{ row }">
            <el-button type="primary" @click="Pass(row)" :disabled="row.status == 0">通过审核</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-pagination
      v-model:current-page="queryForm.pageNum"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      layout="prev, pager, next, jumper, sizes, total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :background="true"
    />
  </el-card>
  <Dialog v-model="dialogVisible"
          :dialogTitle="dialogTitle"
          v-if="dialogVisible"
          @query="query"
          :dialogRowValue="dialogRowValue" />
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Search, Edit, Delete } from '@element-plus/icons-vue'
import { change, list, remove, replace } from '../../../api/accessories'
import { save } from '../../../api/inventory'
import Dialog from './dialog'
import { ElMessageBox, ElMessage } from 'element-plus'

const queryForm = ref({
  accessoriesId: '',
  accessoriesType: '',
  storeId: '',
  supplyId: '',
  username: '',
  purchase: '',
  pageNum: 1,
  pageSize: 10
})
const tableData = ref([])
const total = ref(0)
const dialogVisible = ref(false)
const dialogTitle = ref('')

async function query() {
  const resp = await list(queryForm.value)
  tableData.value = resp.data.data.list
  total.value = resp.data.data.total
}

onMounted(() => {
  query()
})

function handleCurrentChange(pageNum) {
  queryForm.value.pageNum = pageNum
  query()
}

function handleSizeChange(pageSize) {
  queryForm.value.pageNum = 1
  queryForm.value.pageSize = pageSize
  query()
}

const dialogRowValue = ref({})
// 审核
function Pass(row) {
  // console.log(row.id);
  ElMessageBox({
    title: '审核框', // MessageBox 标题
    message: '是否确定通过此审核项?', // MessageBox 消息正文内容
    confirmButtonText: '确定', // 确定按钮的文本内容
    cancelButtonText: '取消', // 取消按钮的文本内容
    showCancelButton: true, // 是否显示取消按钮
    closeOnClickModal: false, // 是否可通过点击遮罩关闭
    type: 'warning' // 消息类型，用于显示图标
  }).then(async () => {
    await change(row.id)
    query()
    ElMessage.success('审核通过!')
  }).catch(() => {
    ElMessage.info('已取消审核!')
  })
}
// 完成此采购
function Purchase(row) {
  // console.log(row.id);
  ElMessageBox({
    title: '完成采购框', // MessageBox 标题
    message: '是否确定通过此采购项?', // MessageBox 消息正文内容
    confirmButtonText: '确定', // 确定按钮的文本内容
    cancelButtonText: '取消', // 取消按钮的文本内容
    showCancelButton: true, // 是否显示取消按钮
    closeOnClickModal: false, // 是否可通过点击遮罩关闭
    type: 'warning' // 消息类型，用于显示图标
  }).then(async () => {
    queryForm.value.supplyId = row.supplyId
    queryForm.value.accessoriesId = row.id
    queryForm.value.accessoriesType = row.accessoriesType
    queryForm.value.storeId = row.storeId
    console.log(queryForm.value)
    await save(queryForm.value)
    await replace(row.id)
    query()
    ElMessage.success('采购通过通过!')
    // 点击按钮禁用它
  }).catch(() => {
    ElMessage.info('已取消采购!')
  })
}
// 添加
function handleAdd() {
  dialogTitle.value = '添加'
  dialogRowValue.value = {}
  dialogVisible.value = true
}
// 编辑
function handleEdit(row) {
  dialogTitle.value = '编辑'
  dialogRowValue.value = JSON.parse(JSON.stringify(row))
  dialogVisible.value = true
}
// 删除
function removeById(row) {
  ElMessageBox.confirm(
    '是否确认删除?',
    '确认框',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消'
    }
  ).then(async () => {
    await remove(row.id)
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
    query()
  }).catch(() => {
    /*
    ElMessage({
      type: 'info',
      message: 'Delete canceled'
    })
     */
  })
}
// 计算总价
function calculate(row) {
  return row.total * row.price + '元'
}
// 状态
function formatStatus(row, column) {
  const status = row[column.property]
  if (status === '1') {
    return '审核中'
  } else if (status === '0') {
    return '审核成功'
  } else if (status === '2') {
    return '未通过'
  }
}
// 状态
function formatPurchase(row, column) {
  const purchase = row[column.property]
  if (purchase === '1') {
    return '已采购'
  } else if (purchase === '0') {
    return '未采购'
  }
}
// 时间
function formatTime(row, column) {
  const date = new Date(row[column.property])
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month < 10) {
    month = '0' + month
  }
  let day = date.getDate()
  if (day < 10) {
    day = '0' + day
  }
  return year + '-' + month + '-' + day
}
</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}
::v-deep .el-input__suffix {
  align-items: center;
}
</style>
